/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;

/**
 *
 * @author AdminNUS
 */
@ManagedBean(name="salesOrderReportPDFBean")
//@Named(value = "salesOrderReportPDFBean")
@RequestScoped
public class SalesOrderReportPDFBean {
    @Resource(name = "macteamData")
    private DataSource macteamData;

    /** Creates a new instance of SalesOrderReportPDFBean */
    public SalesOrderReportPDFBean() {
    }
    
    public void exportReportToPdf() throws JRException, FileNotFoundException, IOException {

        Connection conn = null;
        JasperReport jasperreport;
        JasperPrint jasperprint;

        try {
            conn = macteamData.getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(ExportToPDFBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        HashMap jasperParameter = new HashMap();

        //jasperreport = JasperCompileManager.compileReport(System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/MacTeam/MacTeam-war/web/jasperreports/salesLeadReport.jrxml");

        //jasperprint = JasperFillManager.fillReport(jasperreport, jasperParameter, conn);

        //JasperExportManager.exportReportToPdfFile(jasperprint, System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/MacTeam/MacTeam-war/web/jasperreports/testReport.pdf");

//        //New Code
//        JasperReport jasperReport = JasperCompileManager.compileReport(System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/trunk/MacTeam/MacTeam-war/web/jasperreports/6amReport.jrxml");
//        JasperPrint  jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter, conn);
//        //JasperExportManager.exportReportToPdfFile(jasperPrint, System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/trunk/MacTeam/dist/gfdeploy/MacTeam/MacTeam-war_war/jasperreports/testConvertedCustomerReport2.pdf");
//        JasperExportManager.exportReportToPdfFile(jasperPrint, System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/trunk/MacTeam/MacTeam-war/web/jasperreports/testSalesOrderReport2.pdf");
//        System.out.println("********" + FacesContext.getCurrentInstance().getExternalContext().getRealPath("/"));
//        FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "/jasperreports/testSalesOrderReport2.pdf?faces-redirect=true");
//        
//        showPDF(); 
        //New Code
        JasperReport jasperReport = JasperCompileManager.compileReport(System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/trunk/MacTeam/MacTeam-war/web/jasperreports/6amReport.jrxml");
        //JasperReport jasperReport = JasperCompileManager.compileReport(System.getProperty("user.home")+"/MacTeam/MacTeam-war/web/jasperreports/convertedCustomerReport3.jrxml");
        JasperPrint  jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter, conn);
        //JasperExportManager.exportReportToPdfFile(jasperPrint, System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/trunk/MacTeam/dist/gfdeploy/MacTeam/MacTeam-war_war/jasperreports/testConvertedCustomerReport2.pdf");
        JasperExportManager.exportReportToPdfFile(jasperPrint, System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/trunk/MacTeam/MacTeam-war/web/jasperreports/testSalesOrderReport2.pdf");
        //JasperExportManager.exportReportToPdfFile(jasperPrint, System.getProperty("user.home")+"/MacTeam/MacTeam-war/web/jasperreports/testConvertedCustomerReport2.pdf");
        System.out.println("********" + FacesContext.getCurrentInstance().getExternalContext().getRealPath("/"));
        FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "/jasperreports/testSalesOrderReport2.pdf?faces-redirect=true");
        //FacesContext.getCurrentInstance().getExternalContext().redirect("/jasperreports/testConvertedCustomerReport2.pdf");
        //FacesContext.getCurrentInstance().getExternalContext().dispatch("/jasperreports/testConvertedCustomerReport2.pdf");
        //FacesContext.getCurrentInstance().getExternalContext().redirect("/jasperreports/testConvertedCustomerReport2.pdf");
     
        pleaseShowPDF(); 
                
    }
    
    public void pleaseShowPDF() {
        
        try
{
            String strSQL = "SELECT saleorderreport.`QUANTITY`, saleorderreport.`TYPE`, saleorderreport.`MONTHOFSALES` AS MONTH_OF_SALES, saleorderreport.`YEAROFSALES` AS YEAR_OF_SALES " +
      			"FROM `saleorderreport` saleorderreport"; 

            ResultSet rsEvents = macteamData.getConnection().prepareStatement(strSQL).executeQuery();
            JRResultSetDataSource jrResultSetDataSource = new JRResultSetDataSource(rsEvents);

            //InputStream reportStream = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true)).getServletContext().getResourceAsStream(null);
            InputStream reportStream = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/jasperreports/6amReport.jasper");
            
            ((HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()).setContentType("application/pdf");     
            ServletOutputStream outputStream = ((HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()).getOutputStream();
            HashMap jasperParameter = new HashMap();
        
            JasperRunManager.runReportToPdfStream(reportStream, outputStream, jasperParameter, jrResultSetDataSource);
            

            outputStream.flush();

            outputStream.close();
            
            }

        catch (JRException jrex)

        {

            System.out.println("********** Jasperreport Exception");

            jrex.printStackTrace();

        }

        catch (Exception ex)

        {

            ex.printStackTrace();

        }
}
    
    
}


